/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.bean.util.erp;

import cn.ekuma.epos.businesslogic.linecalc.DefaultLineCalcLogic;
import cn.ekuma.epos.businesslogic.linecalc.ServingLineCalcLogic;
import com.openbravo.bean.Serving;
import com.openbravo.format.Formats;
import com.openbravo.bean.shard.I_LineCalcLogic;
import com.openbravo.bean.erp.OrderLine;
import com.openbravo.pos.util.RoundUtils;
import com.openbravo.pos.util.StringUtils;

/**
 *
 * @author Administrator
 */
public class OrderLineHelper {

     static I_LineCalcLogic defaultLineCalcLogic=new DefaultLineCalcLogic();
     static I_LineCalcLogic serviceLineCalcLogic=new ServingLineCalcLogic();

     public static String printName(OrderLine l) {
        if(l.isPrefSell)
            return "*"+StringUtils.encodeXML(l.getProperty("product.name"));
        return StringUtils.encodeXML(l.getProperty("product.name"));
    }

    public static String printMultiply(OrderLine l) {
        return Formats.DOUBLE.formatValue(getMultiply(l));
    }

    public static String printPrice(OrderLine l) {
        return Formats.CURRENCY.formatValue(getRealPrice(l));
    }

    public static String printDefaultPrice(OrderLine l) {
        return Formats.CURRENCY.formatValue(getRealDefaultPrice(l));
    }
    
    public static String printTaxRate(OrderLine l) {
        return Formats.PERCENT.formatValue(l.getTaxRate());
    }

    public static String printPriceRate(OrderLine l){
        return Formats.PERCENT.formatValue(l.priceRate);
    }

    public static String printConsulMultiply(OrderLine l){
        return Formats.DOUBLE.formatValue(l.getConsulMultiply());
    }

    //实价
    public static  double getPriceTax(OrderLine l) {
        return RoundUtils.multiply(getRealPrice(l) , (1.0 + l.getTaxRate()));
    }
    
    public static double getSubValue(OrderLine l) {
        return RoundUtils.multiply(getRealPrice(l) ,getMultiply(l),l.priceRate);
    }

    public static double getTax(OrderLine l) {
        return RoundUtils.multiply(getSubValue(l) , l.getTaxRate());
    }
    
    public static double getValue(OrderLine l) {
        return RoundUtils.multiply(getSubValue(l) ,1.0 + l.getTaxRate());
    }
    
    public static String printPriceTax(OrderLine l) {
        return Formats.CURRENCY.formatValue(getPriceTax(l));
    }
    
    public static String printTax(OrderLine l) {
        return Formats.CURRENCY.formatValue(getTax(l));
    }
    
    public static String printSubValue(OrderLine l) {
        return Formats.CURRENCY.formatValue(getSubValue(l));
    }

    public static String printValue(OrderLine l) {
        return Formats.CURRENCY.formatValue(getValue(l));
    }
    

    //原价
    public static double getDefaultSubValue(OrderLine l) {
        return RoundUtils.multiply(getRealDefaultPrice(l) , getMultiply(l));
    }

    public static double getDefaultTax(OrderLine l) {
        return RoundUtils.multiply(getDefaultSubValue(l) , l.getTaxRate());
    }
    
    public static double getDefaultValue(OrderLine l) {
        return RoundUtils.multiply(getDefaultSubValue(l) , 1.0 + l.getTaxRate());
    }
    
    public static String printDefaultSubValue(OrderLine l) {
        return Formats.CURRENCY.formatValue(getDefaultSubValue(l));
    }

    public static String printDefaultValue(OrderLine l) {
        return Formats.CURRENCY.formatValue(getDefaultValue(l));
    }
    
    
    //差价
    public static double getPrefSubValue(OrderLine l){
        return RoundUtils.substract(getDefaultSubValue(l),getSubValue(l));
    }

    public static  double getPrefValue(OrderLine l){
        return  RoundUtils.multiply(getPrefSubValue(l), 1.0+l.getTaxRate());
    }

    public static String printPrefSubValue(OrderLine l) {
        return Formats.CURRENCY.formatValue(getPrefSubValue(l));
    }

    public static String printPrefValue(OrderLine l) {
        return Formats.CURRENCY.formatValue(getPrefValue(l));
    }
    

    public static I_LineCalcLogic getTicketLineCalcLogic(OrderLine l){
        if(l.isServiceOrderLine())
            return serviceLineCalcLogic;
        return defaultLineCalcLogic;
    }

    public static double getMultiply(OrderLine l) {
        return getTicketLineCalcLogic(l).getMultiply(l);
    }

    public static Serving getServingByOrderLine(OrderLine l){
        Serving obj=new Serving();
        obj.setM_sServiceId(l.getServiceId());
        obj.setRantCycleStyle(l.getRantCycleStyle());
        obj.setCyclePrice(l.getPrice());
        obj.setPriceRate(l.getPriceRate());
        obj.setRantData(l.getStartData());
        obj.setReturnData(l.getEndData());
        return obj;
    }


	public static double getConsultValue(OrderLine line) {
		return RoundUtils.multiply(getRealConsultPrice(line),getMultiply(line));
	}
 
	public static double getRealPrice(OrderLine l) {
        return RoundUtils.add(l.getPrice(),l.getAttrPrice());
    }
	
	public static double getRealConsultPrice(OrderLine l) {
        return RoundUtils.add(l.getConsultPrice(),l.getAttrPriceCost());
    }
	
	public static double getRealDefaultPrice(OrderLine l) {
        return RoundUtils.add(l.getDefaultPrice(),l.getAttrPrice());
    }
	
	public static String getText(OrderLine l){
        String sHtml = "<tr><td >"+ "["+ printName(l) +"]" +"</td></tr><tr>" +
                "<td align=\"center\" width=\"10\">"+ printMultiply(l) +"</td>" +
                "<td width=\"150\">"+ printPrice(l) +"</td>" +
                "<td align=\"center\" width=\"100\">Tax:"+ printTaxRate(l) +"</td>" +
                "<td align=\"center\" width=\"100\">折扣："+ printPriceRate(l) +"</td></tr>";
        return sHtml;
    }
}
